import random 
jk = int(input('Введите четырёхзначное число: ')) 
max = 0 
if jk > 9999 or jk < 1000: 
    print ('0') 
def prost(n): 
    if n <= 1: 
        return False 
    for _ in range(5): 
        t = random.randint(1, n-1) 
        if pow(t, n-1, n) != 1: 
            return False 
        return True  
 
 
m = jk // 1000 
if prost(m) == True: 
    max = m#1 
 
 
li = jk // 100 
if prost(li) == True and li > max: 
    max =li#2 
 
 
fb = jk // 10 
if prost(fb) == True and fb > max: 
    max =fb#3 
 
 
t = jk 
if prost(t) == True and t > max: 
    max =t#4

    
dh = jk // 10 % 100 
if prost(dh ) == True and dh > max: 
    max =dh#5
 
g = jk // 100 % 10 
if prost(g) == True and g > max: 
    max = g#6
 
 
our = jk % 1000 
if prost(our) == True and our > max: 
    max = our#7 
 
 
nc = jk % 100 
if prost(nc) == True and nc > max: 
    max = nc#8

    
xz = jk // 10 %10 
if prost(xz) == True and xz > max: 
    max = xz#9
 
y = jk % 10 
if prost(y) == True and y > max: 
    max = y#10 
 
 
print(max)

